﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Boomerang.Common.Sms.BoomerangService;
using Microsoft.Crm.SdkTypeProxy;
using System.Globalization;

namespace Boomerang.Common.Crm
{
    public class CrmSms
    {
        public const string SchemaName = "tmc_sms";
        public static string[] Columns = new string[] { "tmc_smsid", "tmc_contactid", "tmc_subject", "tmc_body", "tmc_contactid", "tmc_date", "tmc_mobilephone", "tmc_responseid", "statuscode" };

        #region Constructors
        public CrmSms(DynamicEntity crmEntity)
        {
            if (crmEntity.Name != SchemaName)
                throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture,"Cannot build a CrmSms from entity {0}", crmEntity.Name));

            if (crmEntity.Properties.Contains("tmc_smsid"))
                this.CrmSmsId = ((Key)crmEntity["tmc_smsid"]).Value;

            if (crmEntity.Properties.Contains("tmc_body"))
                this.TextMessage = ((string)crmEntity["tmc_body"]);

            if (crmEntity.Properties.Contains("tmc_date"))
                this.DateProcessed = DateTime.Parse(((CrmDateTime)crmEntity["tmc_date"]).Value,CultureInfo.InvariantCulture);

            if (crmEntity.Properties.Contains("tmc_mobilephone"))
                this.MobilePhone = ((string)crmEntity["tmc_mobilephone"]);

            if (crmEntity.Properties.Contains("statuscode"))
                this.DeliveryStatus = (CrmSmsDeliveryStatus)((Status)crmEntity["statuscode"]).Value;

            if (crmEntity.Properties.Contains("tmc_contactid"))
                this.CrmContactId = ((Lookup)crmEntity["tmc_contactid"]).Value;

            if (crmEntity.Properties.Contains("tmc_responseid"))
                this.CrmResponseId = ((Lookup)crmEntity["tmc_responseid"]).Value;

            if (crmEntity.Properties.Contains("tmc_subject"))
                this.Subject = ((string)crmEntity["tmc_subject"]);

        }

        public CrmSms(Boomerang.Common.Sms.BoomerangService.Response boomerangResponse)
        {
            this.TextMessage = boomerangResponse.Text;
            this.MobilePhone = boomerangResponse.RecipientsMobileNumber;
            this.DeliveryStatus = CrmSmsDeliveryStatus.Received;
            this.DateProcessed = DateTime.Now.ToUniversalTime();
            this.Subject = "Response: " + boomerangResponse.ResponseId;
        }
        #endregion

        #region Properties
        public Guid CrmSmsId { get; set; }

        public Guid CrmContactId { get; set; }

        public Guid CrmResponseId { get; set; }

        public string TextMessage { get; set; }

        public string MobilePhone { get; set; }

        public string Subject { get; set; }

        public CrmSmsDeliveryStatus DeliveryStatus { get; set; }

        public DateTime DateProcessed { get; set; }

        #endregion

        public DynamicEntity ToCrmDynamicEntity()
        {
            DynamicEntity entity = new DynamicEntity();
            entity.Name = SchemaName;
            entity.Properties = new PropertyCollection();

            if (CrmSmsId != Guid.Empty)
                entity.Properties.Add(new KeyProperty("tmc_smsid", new Key(CrmSmsId)));

            if (!string.IsNullOrEmpty(TextMessage))
                entity.Properties.Add(new StringProperty("tmc_body", TextMessage));

            if (CrmContactId != Guid.Empty)
                entity.Properties.Add(new LookupProperty("tmc_contactid", new Lookup(EntityName.contact.ToString(), CrmContactId)));

            if (DateProcessed != null)
                entity.Properties.Add(new CrmDateTimeProperty("tmc_date", new CrmDateTime(DateProcessed.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss K",CultureInfo.InvariantCulture))));

            if (!string.IsNullOrEmpty(MobilePhone))
                entity.Properties.Add(new StringProperty("tmc_mobilephone", MobilePhone));

            if (CrmResponseId != Guid.Empty)
                entity.Properties.Add(new LookupProperty("tmc_responseid", new Lookup(SchemaName, CrmResponseId)));

            if (!string.IsNullOrEmpty(Subject))
                entity.Properties.Add(new StringProperty("tmc_subject", Subject));

            entity.Properties.Add(new StatusProperty("statuscode", new Status((int)DeliveryStatus)));

            return entity;

        }

        #region Cast Operators
        public static implicit operator CrmSms(DynamicEntity entity)
        {
            return new CrmSms(entity);
        }

        public static implicit operator DynamicEntity(CrmSms sms)
        {
            return sms.ToCrmDynamicEntity();
        }

        public static implicit operator CrmSms(BusinessEntity entity)
        {
            DynamicEntity obj = entity as DynamicEntity;
            if (obj == null) return null;
            if (obj.Name != SchemaName) return null;

            return new CrmSms(obj);
        }

        #endregion

    }

    public enum CrmSmsDeliveryStatus
    {
        Pending = 1,
        Sent = 3,
        Received = 4//,
        // Processing = 5,
        // Failed = 6
    }
}
